package com.zdsoft.blp.zongbuju.shouye.linliquan.fragment;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.myFragment.CustomFragment;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.LLPinCheActivitv;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.LLShiChangActivitv;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.XQPCActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.PinCheXinXiAdapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.ZZLYadapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.PinCheEntity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/2.
 */
public class PCfragmentLeft extends CustomFragment implements NetCallback,XListView.IXListViewListener{
    private XListView xlv;
    private String name,id,qidian,zhongdian,fbtime,lianxiren,dianhua,cheliang,miaoshu,kstime,touxiang;
    private int pageNo=1;
    private List<PinCheEntity>list;
    private List<PinCheEntity>mlist;
    private PinCheXinXiAdapter adapter;
    private int source;
    @Override
    protected void init(){
        initLayout(R.layout.linli_fragment_pc);
        xlv=find(R.id.xlv_ll_pc);
        xlv.setXListViewListener(this);
        xlv.setPullRefreshEnable(true);//设置可以刷新
        xlv.setPullLoadEnable(true);//设置可以加载更多
        LLPinCheActivitv ac= (LLPinCheActivitv) getActivity();
        source=ac.source;
        getNet();//实例化net的方法
//        Listview 监听
        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long Id) {
                Intent intent = new Intent(getActivity(), XQPCActivity.class);
                intent.putExtra("id",mlist.get(position-1).getId());
                startActivity(intent);

            }
        });
    }


//回调函数
    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("RES",res);
        if (flag){
            if (pageNo==1){
                mlist=jiexi(code,res);
                adapter = new PinCheXinXiAdapter(mlist,getActivity());
                xlv.setAdapter(adapter);
                xlv.stopRefresh();
            }else {
                list=jiexi(code,res);
                for (PinCheEntity linLis:list){
                    mlist.add(linLis);
                }
                adapter.setListData(mlist);
                xlv.stopLoadMore();
            }
        }
    }

    private List<PinCheEntity> jiexi(int code, String res) {
        list=new ArrayList<>();
        PinCheEntity entity=null;
        try {
            JSONObject object=new JSONObject(res);

            boolean rows=object.isNull("rows");
            if (rows==false){
                JSONArray array=object.getJSONArray("rows");
                for (int i=0;i<array.length();i++){
                    JSONObject obj=array.getJSONObject(i);
                    id=obj.getString("id");
                    name=obj.getString("userName");
                    qidian=obj.getString("start");
                    zhongdian=obj.getString("end");
                    fbtime=obj.getString("creatTime");
                    lianxiren=obj.getString("contact");
                    dianhua=obj.getString("contactPhone");
                    cheliang=obj.getString("carInfo");
                    kstime=obj.getString("startTime");
                    miaoshu=obj.getString("content");
                    touxiang=obj.getString("headimgurl");
                    int approvedResult=obj.getInt("approvedResult");
                    if (code==1){
                        entity=new PinCheEntity(qidian,zhongdian,lianxiren,dianhua,cheliang,miaoshu,kstime,fbtime,touxiang,name,id);
                        list.add(entity);
                    }
                    if (code==2){
                        entity=new PinCheEntity(qidian,zhongdian,lianxiren,dianhua,cheliang,miaoshu,kstime,fbtime,touxiang,name,id,approvedResult);
                        entity.setCode(2);
                        list.add(entity);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    //XListview 监听刷新
    @Override
    public void onRefresh() {
        mlist=new ArrayList<>();
        pageNo=1;
        getNet();
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        getNet();
    }
//实例化Net
    public void getNet() {

        Net net=new Net();
        net.url= API.CARPOOLINGINFO_URL;
        net.nameSpace=API.CARPOOLINGINFO_NAMESPACE;
        Map<String,Object>map=new HashMap<>();
        if (source==2){
            net.methodName=API.MYCARPOOLINGINFOLIST;
            map.put("pageNo",pageNo);
            map.put("pageSize",10);
            map.put("communityCode","00001");
            map.put("type","zck");
            map.put("condition","");
            map.put("curTime","");
            map.put("userId","");
            net.code=2;
            net.parms=map;
        }else {
            net.methodName=API.CARPOOLINGINFOLIST;
            map.put("pageNo",pageNo);
            map.put("pageSize",20);
            map.put("communityCode","00001");
            map.put("type","zck");
            map.put("condition","");
            map.put("curTime","");
            net.code=1;
            net.parms=map;
        }

        ((LLPinCheActivitv)getActivity()).getServer(net,this);
    }
}
